home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.3 / Printer / src / EpsonX / render.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  8.6 KB  |  332 lines

  1. /*
  2.     EpsonX (EX/FX/JX/LX/MX/RX) driver.
  3.     David Berezowski - October/87.
  4.  
  5.   Copyright (c) 1988 Commodore-Amiga, Inc.
  6.  
  7.   Executables based on this information may be used in software
  8.   for Commodore Amiga computers.  All other rights reserved.
  9.  
  10.   This information is provided "as is"; no warranties are made.
  11.   All use is at your own risk, and no liability or responsibility is assumed.
  12. */
  13.  
  14.  
  15. #include <exec/types.h>
  16. #include <exec/nodes.h>
  17. #include <exec/lists.h>
  18. #include <exec/memory.h>
  19. #include "../printer/printer.h"
  20. #include "../printer/prtbase.h"
  21.  
  22. #define NUMSTARTCMD    7    /* # of cmd bytes before binary data */
  23. #define NUMENDCMD    1    /* # of cmd bytes after binary data */
  24. #define NUMTOTALCMD     (NUMSTARTCMD + NUMENDCMD)    /* total of above */
  25. #define NUMLFCMD    4    /* # of cmd bytes for linefeed */
  26. #define MAXCOLORBUFS    4    /* max # of color buffers */
  27.  
  28. #define STARTLEN    19
  29. #define PITCH        1
  30. #define CONDENSED    2
  31. #define LMARG        8
  32. #define RMARG        11
  33. #define DIREC        15
  34.  
  35. static ULONG TwoBufSize;
  36. static UWORD RowSize, ColorSize, NumColorBufs, dpi_code, spacing;
  37. static UWORD colorcodes[MAXCOLORBUFS];
  38.  
  39. Render(ct, x, y, status)
  40. long ct, x, y, status;
  41. {
  42.     extern void *AllocMem(), FreeMem();
  43.  
  44.     extern struct PrinterData *PD;
  45.     extern struct PrinterExtendedData *PED;
  46.  
  47.     UBYTE *CompactBuf();
  48.     static ULONG BufSize, TotalBufSize, dataoffset;
  49.     static UWORD spacing, colors[MAXCOLORBUFS];
  50.     /*
  51.         00-01    \003P        set pitch (10 or 12 cpi)
  52.         02-02    \022        set condensed fine (on or off)
  53.         03-05    \033W\000    enlarge off
  54.         06-08    \033ln        set left margin to n
  55.         09-11    \033Qn        set right margin to n
  56.         12-12    \015        carriage return
  57.         13-15    \033U1        set uni-directional mode
  58.         16-18    \033t\000    see kludge note below
  59.         Kludge to get this to work on a CBM_MPS-1250  which interprets
  60.         'ESCr' as go into reverse print mode.  The 'ESCt' tells it to
  61.         get out of reverse print mode.  The 'NULL' is ignored by the
  62.         CBM_MPS-1250 and required by all Epson printers as the
  63.         terminator for the 'ESCtNULL' command which means select
  64.         normal char set (which has no effect).
  65.     */
  66.     static UBYTE StartBuf[STARTLEN] =
  67.         "\033P\022\033W\000\033ln\033Qn\015\033U1\033t\000";
  68.     UBYTE *ptr, *ptrstart;
  69.     int i, err;
  70.  
  71.     switch(status) {
  72.         case 0 : /* Master Initialization */
  73.             /*
  74.                 ct    - pointer to IODRPReq structure.
  75.                 x    - width of printed picture in pixels.
  76.                 y    - height of printed picture in pixels.
  77.             */
  78.             RowSize = x;
  79.             ColorSize = RowSize + NUMTOTALCMD;
  80.             if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  81.                 NumColorBufs = MAXCOLORBUFS;
  82.                 colors[0] = ColorSize * 3; /* Black */
  83.                 colors[1] = ColorSize * 0; /* Yellow */
  84.                 colors[2] = ColorSize * 1; /* Magenta */
  85.                 colors[3] = ColorSize * 2; /* Cyan */
  86.                 colorcodes[0] = 4; /* Yellow */
  87.                 colorcodes[1] = 1; /* Magenta */
  88.                 colorcodes[2] = 2; /* Cyan */
  89.                 colorcodes[3] = 0; /* Black */
  90.             }
  91.             else { /* grey-scale or black&white */
  92.                 NumColorBufs = 1;
  93.                 colors[0] = ColorSize * 0; /* Black */
  94.                 colorcodes[0] = 0; /* Black */
  95.             }
  96.             BufSize = ColorSize * NumColorBufs + NUMLFCMD;
  97.             if (PED->ped_YDotsInch == 216) {
  98.                 TwoBufSize = BufSize * 3;
  99.                 TotalBufSize = BufSize * 6;
  100.             }
  101.             else if (PED->ped_YDotsInch == 144) {
  102.                 TwoBufSize = BufSize * 2;
  103.                 TotalBufSize = BufSize * 4;
  104.             }
  105.             else {
  106.                 TwoBufSize = BufSize * 1;
  107.                 TotalBufSize = BufSize * 2;
  108.             }
  109.             PD->pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
  110.             if (PD->pd_PrintBuf == NULL) {
  111.                 err = PDERR_BUFFERMEMORY;
  112.             }
  113.             else {
  114.                 dataoffset = NUMSTARTCMD;
  115.                 /*
  116.                     This printer prints graphics within its
  117.                     text margins.  This code makes sure the
  118.                     printer is in 10 cpi and then sets the
  119.                     left and right margins to their minimum
  120.                     and maximum values (respectively).  A
  121.                     carriage return is sent so that the
  122.                     print head is at the leftmost position
  123.                     as this printer starts printing from
  124.                     the print head's position.  The printer
  125.                     is put into unidirectional mode to
  126.                     reduce wavy vertical lines.
  127.                 */
  128.                 StartBuf[PITCH] = 'P'; /* 10 cpi */
  129.                 StartBuf[CONDENSED] = '\022'; /* off */
  130.                 /* left margin of 1 */
  131.                 StartBuf[LMARG] = 0;
  132.                 /* right margin of 80 or 136 */
  133.                 StartBuf[RMARG] = PD->pd_Preferences.
  134.                     PaperSize == W_TRACTOR ? 136 : 80;
  135.                 /* uni-directional mode */
  136.                 StartBuf[DIREC] = '1';
  137.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  138.             }
  139.             break;
  140.  
  141.         case 1 : /* Scale, Dither and Render */
  142.             /*
  143.                 ct    - pointer to PrtInfo structure.
  144.                 x    - 0.
  145.                 y    - row # (0 to Height - 1).
  146.             */
  147.             Transfer(ct, y, &PD->pd_PrintBuf[dataoffset], colors,
  148.                 BufSize);
  149.             err = PDERR_NOERR; /* all ok */
  150.             break;
  151.  
  152.         case 2 : /* Dump Buffer to Printer */
  153.             /*
  154.                 ct    - 0.
  155.                 x    - 0.
  156.                 y    - # of rows sent (1 to NumRows).
  157.  
  158.             */
  159.             /* white-space strip */
  160.             ptrstart = &PD->pd_PrintBuf[dataoffset - NUMSTARTCMD];
  161.             if (PED->ped_YDotsInch == 72) {
  162.                 /* y range : 1 to 8 */
  163.                 y = y * 3 - spacing;
  164.                 ptr = CompactBuf(ptrstart + NUMSTARTCMD,
  165.                     ptrstart, y, 1);
  166.             }
  167.             else if (PED->ped_YDotsInch == 144) {
  168.                 /* y range : 1 to 16 */
  169.                 ptr = CompactBuf(ptrstart + NUMSTARTCMD,
  170.                     ptrstart, 2, 1);
  171.                 if (y > 1) {
  172.                     ptr = CompactBuf(&PD->pd_PrintBuf[
  173.                         dataoffset + BufSize],
  174.                         ptr, y * 3 / 2 - 2, 0);
  175.                 }
  176.             }
  177.             else if (PED->ped_YDotsInch == 216) {
  178.                 /* y range : 1 to 24 */
  179.                 ptr = CompactBuf(ptrstart + NUMSTARTCMD,
  180.                     ptrstart, 1, 1);
  181.                 if (y > 1) {
  182.                     ptr = CompactBuf(&PD->pd_PrintBuf[
  183.                         dataoffset + BufSize],
  184.                         ptr, 1, 0);
  185.                 }
  186.                 if (y > 2) {
  187.                     ptr = CompactBuf(&PD->pd_PrintBuf[
  188.                         dataoffset + BufSize * 2],
  189.                         ptr, y - 2, 0);
  190.                 }
  191.             }
  192.             err = (*(PD->pd_PWrite))(ptrstart, ptr - ptrstart);
  193.             if (err == PDERR_NOERR) {
  194.                 dataoffset = (dataoffset == NUMSTARTCMD ?
  195.                     TwoBufSize : 0) + NUMSTARTCMD;
  196.             }
  197.             break;
  198.  
  199.         case 3 : /* Clear and Init Buffer */
  200.             /*
  201.                 ct    - 0.
  202.                 x    - 0.
  203.                 y    - 0.
  204.             */
  205.             ClearAndInit(&PD->pd_PrintBuf[dataoffset]);
  206.             err = PDERR_NOERR;
  207.             break;
  208.  
  209.         case 4 : /* Close Down */
  210.             /*
  211.                 ct    - error code.
  212.                 x    - io_Special flag from IODRPReq.
  213.                 y    - 0.
  214.             */
  215.             err = PDERR_NOERR; /* assume all ok */
  216.             /* if user did not cancel print */
  217.             if (ct != PDERR_CANCEL) {
  218.                 /* restore preferences pitch and margins */
  219.                 if (PD->pd_Preferences.PrintPitch == ELITE) {
  220.                     StartBuf[PITCH] = 'M'; /* 12 cpi */
  221.                 }
  222.                 else if (PD->pd_Preferences.PrintPitch == FINE) {
  223.                     StartBuf[CONDENSED] = '\017'; /* on */
  224.                 }
  225.                 StartBuf[LMARG] =
  226.                     PD->pd_Preferences.PrintLeftMargin - 1;
  227.                 StartBuf[RMARG] =
  228.                     PD->pd_Preferences.PrintRightMargin;
  229.                 StartBuf[DIREC] = '0'; /* bi-directional */
  230.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  231.             }
  232.             (*(PD->pd_PBothReady))();
  233.             if (PD->pd_PrintBuf != NULL) {
  234.                 FreeMem(PD->pd_PrintBuf, TotalBufSize);
  235.             }
  236.             break;
  237.  
  238.         case 5 :  /* Pre-Master Initialization */
  239.             /*
  240.                 ct    - 0 or pointer to IODRPReq structure.
  241.                 x    - io_Special flag from IODRPReq.
  242.                 y    - 0.
  243.             */
  244.             /* kludge for sloppy tractor mechanism */
  245.             spacing = PD->pd_Preferences.PaperType == SINGLE ?
  246.                 1 : 0;
  247.             dpi_code = SetDensity(x & SPECIAL_DENSITYMASK);
  248.             err = PDERR_NOERR;
  249.             break;
  250.     }
  251.     return(err);
  252. }
  253.  
  254. UBYTE *CompactBuf(ptrstart, ptr2start, y, flag)
  255. UBYTE *ptrstart, *ptr2start;
  256. long y;
  257. int flag; /* 0 - not first pass, !0 - first pass */
  258. {
  259.     static int x;
  260.     UBYTE *ptr, *ptr2;
  261.     long ct;
  262.     int i;
  263.  
  264.     ptr2 = ptr2start; /* where to put the compacted data */
  265.     if (flag) {
  266.         x = 0; /* flag no transfer required yet */
  267.     }
  268.     for (ct=0; ct<NumColorBufs; ct++, ptrstart += ColorSize) {
  269.         i = RowSize;
  270.         ptr = ptrstart + i - 1;
  271.         while (i > 0 && *ptr == 0) {
  272.             i--;
  273.             ptr--;
  274.         }
  275.         if (i != 0) { /* if data */
  276.             *(++ptr) = 13;            /* <cr> */
  277.             ptr = ptrstart - NUMSTARTCMD;
  278.             *ptr++ = 27;
  279.             *ptr++ = 'r';
  280.             *ptr++ = colorcodes[ct];    /* color */
  281.             *ptr++ = 27;
  282.             *ptr++ = dpi_code;        /* density */
  283.             *ptr++ = i & 0xff;
  284.             *ptr++ = i >> 8;        /* size */
  285.             i += NUMTOTALCMD;
  286.             if (x != 0) { /* if must transfer data */
  287.                 /* get src start */
  288.                 ptr = ptrstart - NUMSTARTCMD;
  289.                 do { /* transfer and update dest ptr */
  290.                     *ptr2++ = *ptr++;
  291.                 } while (--i);
  292.             }
  293.             else { /* no transfer required */
  294.                 ptr2 += i; /* update dest ptr */
  295.             }
  296.         }
  297.         if (i != RowSize + NUMTOTALCMD) { /* if compacted or 0 */
  298.             x = 1; /* flag that we need to transfer next time */
  299.         }
  300.     }
  301.     *ptr2++ = 13; /* cr */
  302.     *ptr2++ = 27;
  303.     *ptr2++ = 'J';
  304.     *ptr2++ = y; /* y/216 lf */
  305.     return(ptr2);
  306. }
  307.  
  308. ClearAndInit(ptr)
  309. UBYTE *ptr;
  310. {
  311.     ULONG *lptr, i, j;
  312.  
  313.     /*
  314.         Note : Since 'NUMTOTALCMD + NUMLFCMD' is > 3 bytes if is safe
  315.         to do the following to speed things up.
  316.     */
  317.     i = TwoBufSize - NUMTOTALCMD - NUMLFCMD;
  318.     j = (ULONG)ptr;
  319.     if (!(j & 1)) { /* if on a word boundary, clear by longs */
  320.         i = (i + 3) / 4;
  321.         lptr = (ULONG *)ptr;
  322.         do {
  323.             *lptr++ = 0;
  324.         } while (--i);
  325.     }
  326.     else { /* clear by bytes */
  327.         do {
  328.             *ptr++ = 0;
  329.         } while (--i);
  330.     }
  331. }
  332.